package ESP.Control.L;

import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import ESP.Dao.BaseDao;
import ESP.Util.Encryption;
import ESP.VO.User;
import com.opensymphony.xwork2.ActionContext;

@Controller
public class Login {
	@Resource
	public BaseDao baseDao;
	private Map<String, String> message = new HashMap<String, String>();
	
	private String user_id;
	private String user_password;
	/**
	 * 登录
	 * @param user_id
	 * @param user_password
	 * @return
	 * @throws Exception 
	 */
	public String login() throws Exception{
		User user;
		user = baseDao.login(user_id, Encryption.encryptionSHA1(user_password));
		if(user == null){
			baseDao.setReturnInfo(this.message, "N", "登录失败,请确认用户名及密码输入正确!");
		}else {
			Map<String , Object> session = ActionContext.getContext().getSession();//登录信息放进session
			session.put("user_id",user.getUser_id());
			session.put("user_name",user.getUser_name());
			session.put("user_role_id",user.getUser_role_id());
			if (user_id.equals(Encryption.encryptionSHA1(user_password))) {
				baseDao.setReturnInfo(this.message, "Y", "登录成功!建议您修改初始密码!");
			}else {
				baseDao.setReturnInfo(this.message, "Y", "登录成功!");
			}
		}
		return "login";
	}
	/**
	 * 安全退出
	 * @return
	 */
	public String outLogin(){
		Map<String, Object> session = ActionContext.getContext().getSession();
		session.remove("user_id");
		session.remove("user_name");
		session.remove("user_role_id");
		return "outLogin";
	}
	public BaseDao getBaseDao() {
		return baseDao;
	}
	
	public void setBaseDao(BaseDao baseDao) {
		this.baseDao = baseDao;
	}
	
	public String getUser_id() {
		return user_id;
	}
	public void setUser_id(String userId) {
		user_id = userId;
	}
	public String getUser_password() {
		return user_password;
	}
	public void setUser_password(String userPassword) {
		user_password = userPassword;
	}
	public Map<String, String> getMessage() {
		return message;
	}
	public void setMessage(Map<String, String> message) {
		this.message = message;
	}
	
}
